Dart List operator ==
Syntax & Examples


Syntax of List.operator ==

The syntax of List.operator == operator is:

operator ==(Object other) → bool

This operator == operator of List whether this list is equal to other.

Parameters

ParameterOptional/RequiredDescription
otherrequiredthe object to compare with this list


✐ Examples

1 Check equality of two integer lists

In this example,

  1. We create three lists, list1, list2, and list3, each containing integers.
  2. We then compare list1 with list2 using the == operator, which returns true if the lists have the same elements in the same order.
  3. We also compare list1 with list3, which returns false as the elements are different.
  4. We print the results to standard output.

Dart Program

void main() {
  List<int> list1 = [1, 2, 3];
  List<int> list2 = [1, 2, 3];
  List<int> list3 = [4, 5, 6];
  print(list1 == list2); // Output: true
  print(list1 == list3); // Output: false
}

Output

true
false

2 Check equality of two string lists

In this example,

  1. We create three lists, list1, list2, and list3, each containing strings.
  2. We then compare list1 with list2 using the == operator, which returns true if the lists have the same elements in the same order.
  3. We also compare list1 with list3, which returns false as the elements are different.
  4. We print the results to standard output.

Dart Program

void main() {
  List<String> list1 = ['a', 'b', 'c'];
  List<String> list2 = ['a', 'b', 'c'];
  List<String> list3 = ['x', 'y', 'z'];
  print(list1 == list2); // Output: true
  print(list1 == list3); // Output: false
}

Output

true
false

3 Check equality of two dynamic lists

In this example,

  1. We create three lists, list1, list2, and list3, each containing elements of various types.
  2. We then compare list1 with list2 using the == operator, which returns true if the lists have the same elements in the same order.
  3. We also compare list1 with list3, which returns false as the elements are different.
  4. We print the results to standard output.

Dart Program

void main() {
  List<dynamic> list1 = [1, 'two', 3.0];
  List<dynamic> list2 = [1, 'two', 3.0];
  List<dynamic> list3 = [4, 'five', 6.0];
  print(list1 == list2); // Output: true
  print(list1 == list3); // Output: false
}

Output

true
false

Summary

In this Dart tutorial, we learned about operator == operator of List: the syntax and few working examples with output and detailed explanation for each example.